const express = require('express');
const db = require('../db');
const router = express.Router();

router.get('/getbooks', async(req, res) => {
    let r = await db('select * from student');
    console.log(r)
    if (!r || r.affectedRows === 0) {
        res.send({
            status: 400,
            msg: '获取失败',
        });
    } else {
        res.send({
            status: 200,
            msg: '获取成功',
            data: r
        });
    };
});
router.post('/addbook', async(req, res) => {
    let r = await db('insert into student set ?', req.body);
    if (!r || r.affectedRows === 0) {
        res.send({
            status: 400,
            msg: '添加失败',
        });
    } else {
        res.send({
            status: 200,
            msg: '添加成功',
        });
    };
});
router.get('/delbook', async(req, res) => {
    let r = await db('delete from student where id =?', req.query.id);
    // console.log(r)
    if (!r || r.affectedRows === 0) {
        res.send({
            status: 400,
            msg: '删除失败',
        });
    } else {
        res.send({
            status: 200,
            msg: '删除成功',
        });
    };
});
module.exports = router;